package cn.schoolwow.workflow.test.module.business;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;

import java.util.concurrent.TimeUnit;

public class WorkFlowTaskAuditTimeoutTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        startWorkFlow(flowContext);
        firstAudit(flowContext);
    }

    @Override
    public String name() {
        return "审批任务超时提醒测试用例";
    }

    private void startWorkFlow(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = "审批超期提醒工作流";
        startWorkFlowRequest.initiator = "张三";
        flowContext.putCurrentFlowInstanceData(startWorkFlowRequest);

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);
    }

    private void firstAudit(FlowContext flowContext) throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);

        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowTaskQuery.assignerList.add("部门经理");

        WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
        quickWorkFlow.completeTask(workFlowTaskResponse.taskId,TaskType.COMPLETE);
    }

}
